home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-10-26 | 7.7 KB | 296 lines | [TEXT/MPS ] |
- #
- # filescan.test
- #
- # Tests for the scancontext and scanfile commands.
- #---------------------------------------------------------------------------
- # Copyright 1992-1993 Karl Lehenbauer and Mark Diekhans.
- #
- # Permission to use, copy, modify, and distribute this software and its
- # documentation for any purpose and without fee is hereby granted, provided
- # that the above copyright notice appear in all copies. Karl Lehenbauer and
- # Mark Diekhans make no representations about the suitability of this
- # software for any purpose. It is provided "as is" without express or
- # implied warranty.
- #------------------------------------------------------------------------------
- # $Id: filescan.test,v 2.4 1993/08/03 06:13:44 markd Exp $
- #------------------------------------------------------------------------------
- #
-
- if {[info procs test] != "test"} then {source testlib.tcl}
-
- # Increment a name. This takes a name and "adds one" to it, that is advancing
- # each digit lexically through "0"..."9" -> "A"-"Z" -> "a"..."z". When one
- # digit wraps, the next one is advanced. Optional arg forces upper case only
- # if true and start with all upper case or digits.
-
- proc IncrName {Name args} {
- set Upper [expr {([llength $args] == 1) && [lindex $args 0]}]
- set Last [expr [clength $Name]-1]
- set Begin [csubstr $Name 0 $Last]
- set Digit [cindex $Name $Last]
- set Recurse 0
- case $Digit in {
- {9} {set Digit A}
- {Z} {if {$Upper} {set Recurse 1} else {set Digit a}}
- {z} {set Recurse 1}
- default {set Digit [ctype char [expr [ctype ord $Digit]+1]]}
- }
- if {$Recurse} {
- if {$Last == 0} then {
- return 0 ;# Wrap around
- } else {
- return "[IncrName $Begin]0"
- }
- }
- return "$Begin$Digit"
- }
-
- # Proc to generate record that can be validated. The record has
- # grows quite large to test the dynamic buffering in the file I/O.
-
- proc GenScanRec {Key LineNum} {
- set extra [replicate :@@@@@@@@: $LineNum]
- return "$Key This is a test record ($extra) index is $Key"
- }
-
- # Proc to validate a matched record.
-
- proc ValMatch {scanInfo errId} {
- global testFH matchInfo
-
- Test filescan-${errId}.1 {filescan tests} {
- set matchInfo(line)
- } 0 [GenScanRec [lindex $scanInfo 0] [lindex $scanInfo 2]]
-
- Test filescan-${errId}.2 {filescan tests} {
- set matchInfo(offset)
- } 0 [lindex $scanInfo 1]
-
- Test filescan-${errId}.3 {filescan tests} {
- set matchInfo(linenum)
- } 0 [lindex $scanInfo 2]
-
- Test filescan-${errId}.4 {filescan tests} {
- set matchInfo(handle)
- } 0 $testFH
-
- set matchType [lindex $scanInfo 3]
- global matchCnt.$matchType
- incr matchCnt.$matchType
- }
-
- global matchInfo
- global matchCnt.0 matchCnt.1 matchCnt.2 matchCnt.3 DefaultCnt
- global chkMatchCnt.0 chkMatchCnt.1 chkMatchCnt.2 chkMatchCnt.3 chkDefaultCnt
- global testFH
-
- set matchCnt.0 0
- set matchCnt.1 0
- set matchCnt.2 0
- set matchCnt.3 0
- set defaultCnt 0
- set chkMatchCnt.0 0
- set chkMatchCnt.1 0
- set chkMatchCnt.2 0
- set chkMatchCnt.3 0
- set chkDefaultCnt 0
- set scanList {}
- set maxRec 200
-
- catch {unlink TEST.TMP}
- set testFH [open TEST.TMP w]
-
- # Build a test file and a list of records to scan for. Each element in the
- # list will have the following info:
- # {key fileOffset fileLineNumber matchType}
-
- set key FatHeadAAAA
- for {set cnt 0} {$cnt < $maxRec} {incr cnt} {
- if {($cnt % 10) == 0} {
- set matchType [random 4]
- incr chkMatchCnt.$matchType
- set scanInfo [list "$key [tell $testFH] [expr $cnt+1] $matchType"]
- if {[random 2]} {
- set scanList [concat $scanList $scanInfo]
- } else {
- set scanList [concat $scanInfo $scanList]}
- } else {
- incr chkDefaultCnt}
- if {$cnt == [expr $maxRec/2]} {
- set midKey $key
- }
- puts $testFH [GenScanRec $key [expr $cnt+1]]
- set key [IncrName $key 1] ;# Upper case only
- }
-
- close $testFH
-
- # Build up the scan context.
-
- set testCH [scancontext create]
-
- foreach scanInfo $scanList {
- set key [lindex $scanInfo 0]
- set matchType [lindex $scanInfo 3]
- set cmd "global matchInfo; ValMatch \{$scanInfo\} 1.1"
- case $matchType in {
- {0} {scanmatch -nocase $testCH [string toupper $key] $cmd}
- {1} {scanmatch $testCH ^$key $cmd}
- {2} {scanmatch $testCH $key\$ $cmd}
- {3} {scanmatch $testCH $key $cmd}
- }
- }
-
- scanmatch $testCH {
- global defaultCnt testFH matchInfo
-
- incr defaultCnt
-
- Test filescan-1.2 {filescan tests} {
- set matchInfo(handle)
- } 0 $testFH
- }
-
- set testFH [open TEST.TMP r]
- scanfile $testCH $testFH
-
- Test filescan-1.3 {filescan tests} {
- set {matchCnt.0}
- } 0 ${chkMatchCnt.0}
-
- Test filescan-1.4 {filescan tests} {
- set {matchCnt.1}
- } 0 ${chkMatchCnt.1}
-
- Test filescan-1.5 {filescan tests} {
- set {matchCnt.2}
- } 0 ${chkMatchCnt.2}
-
- Test filescan-1.6 {filescan tests} {
- set {matchCnt.3}
- } 0 ${chkMatchCnt.3}
-
- Test filescan-1.7 {filescan tests} {
- set defaultCnt
- } 0 $chkDefaultCnt
-
- scancontext delete $testCH
-
- # Test return and continue from within match commands
-
- set testCH [scancontext create]
- seek $testFH 0
- global matchCnt
- set matchCnt 0
-
- scanmatch $testCH $midKey {
- global matchCnt
- incr matchCnt
- continue;
- }
-
- scanmatch $testCH ^$midKey {
- error "This should not ever get executed 2.1"
- }
-
- scanmatch $testCH [IncrName $midKey] {
- return "FudPucker"
- }
-
- Test filescan-2.2 {filescan tests} {
- scanfile $testCH $testFH
- } 0 "FudPucker"
-
- scancontext delete $testCH
-
- # Test argument checking and error handling.
-
- Test filescan-3.1 {filescan tests} {
- scancontext foomuch
- } 1 {invalid argument, expected one of: create or delete}
-
- Test filescan-3.2 {filescan tests} {
- scanmatch $testCH
- } 1 {wrong # args: scanmatch ?-nocase? contexthandle ?regexp? command}
-
- Test filescan-3.3 {filescan tests} {
- scanmatch
- } 1 {wrong # args: scanmatch ?-nocase? contexthandle ?regexp? command}
-
- Test filescan-3.4 {filescan tests} {
- scanfile
- } 1 {wrong # args: scanfile contexthandle filehandle}
-
- Test filescan-3.5 {filescan tests} {
- set testCH [scancontext create]
- scanfile $testCH $testFH
- } 1 {no patterns in current scan context}
- catch {scancontext delete $testCH}
-
- close $testFH
-
- #
- # Test subMatch handling.
- #
-
- set testFH [open TEST.TMP w]
- loop idx 0 10 {
- puts $testFH "AAx[replicate xx $idx]xBBc[replicate cc $idx]cDD"
- }
- close $testFH
-
- # Procedure to verify submatches. Works for upper or lower case.
-
- proc ChkSubMatch {id matchInfoVar} {
- upvar $matchInfoVar matchInfo
-
- set idx [expr $matchInfo(linenum) - 1]
-
- set end0 [expr 3+($idx * 2)]
- Test filescan-$id.0.$idx {filescan tests} {
- set matchInfo(submatch0)
- } 0 "x[replicate xx $idx]x"
- Test filescan-$id.1.$idx {filescan tests} {
- set matchInfo(subindex0)
- } 0 "2 $end0"
-
- set start1 [expr $end0+3]
- set end1 [expr $start1+($idx*2)+1]
- Test filescan-$id.2.$idx {filescan tests} {
- set matchInfo(submatch1)
- } 0 "c[replicate cc $idx]c"
- Test filescan-$id.3.$idx {filescan tests} {
- set matchInfo(subindex1)
- } 0 "$start1 $end1"
-
- Test filescan-$id.4.$idx {filescan tests} {
- list [info exists matchInfo(submatch2)] \
- [info exists matchInfo(subindex2)]
- } 0 {0 0}
- }
-
- set testFH [open TEST.TMP r]
-
- set testCH [scancontext create]
- scanmatch $testCH {\A*(x*)B*(c*)DD} {
- ChkSubMatch 4 matchInfo
- }
-
- scanmatch -nocase $testCH {\Aa(x*)B(C*)Dd} {
- ChkSubMatch 5 matchInfo
- }
-
- scanfile $testCH $testFH
-
- close $testFH
- unlink TEST.TMP
-
- rename GenScanRec {}
- rename ValMatch {}
- rename ChkSubMatch {}
-
- unset matchCnt matchInfo
- unset matchCnt.0 matchCnt.1 matchCnt.2 matchCnt.3 defaultCnt
- unset chkMatchCnt.0 chkMatchCnt.1 chkMatchCnt.2 chkMatchCnt.3 chkDefaultCnt
- unset testFH
-